//
//  ModuleFactory.cpp
//  sad
//
//  Created by Christopher Kemsley on 1/31/12.
//  Copyright (c) 2012 iAppolopolis. All rights reserved.
//


#include "ModuleFactory.hpp"

#include "Codec.hpp"
#include "Settings.hpp"
#include "Log.hpp"
#include "Exception.hpp"

using namespace Sim ;




struct ModuleSettings : public Settings
{
	std::string	variant ;
	std::string	name ;
	
	ModuleSettings ()
	:	Settings("Info")
	,	variant ( "Stub" )
	,	name ( "Stub" )
		{
		add ( "variant" , variant ) ;
		add ( "name" , name ) ;
		}
} ;





Model*	ModuleFactory :: create	( ModelVariant const & identity )
	{
	std::map<std::string,ModuleMaker*>::const_iterator it = makers() . find ( identity.builderName ) ;
	if ( it == makers().end() ) return 0 ;
	
	ModuleMaker * p = (*it).second ;
	
	Log::states("ModuleFactory") << "Creating \""<<identity<<"\"" ;
	
	try
		{
		return p -> create ( identity ) ;
		}
	catch ( Exception & e )
		{
		Log :: logException ( e ) ;
		AddExceptionTrace ( e , "ModuleFactory" , "ModuleFactory failed to create " << identity , __FILE__,__LINE__ ) ;
		throw e ;
		return 0 ;
		}
	
	ThrowException ( "ModuleFactory" , "ModuleFactory failed to create " << identity , __FILE__,__LINE__ ) ;
	}


std::map<std::string,ModuleMaker*>&	ModuleFactory :: makers ()
	{
	Log::states("ModuleFactory") << "ModuleFactory :: makers" ;
	static std::map<std::string,ModuleMaker*> m ;
	return m ;
	}

ModuleMaker :: ModuleMaker ( char const* name )
	{
	Log::states("ModuleFactory") << "Registering maker \"" << name << "\" - " << this ;
	ModuleFactory::makers() [name] = this ;
	}

